<html>
    <head>
        <title>订单处理(代理商)</title>
    </head>
    <body>
        <div id="offer_web_order_div" class="x-form-item">
        </div>
        <div id="queryTicketGrid" class="x-form-item">
        </div>
        <script>

            Ext.onReady(function(){
                //############## 订单Grid ##############
                var webOfferOrderCM = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
                        header: '编号',
                        dataIndex: 'id'
                    }, {
                        header: '定单号',
                        dataIndex: 'order_no',
                        width: 120
                    }, {
                        header: 'PNR',
                        dataIndex: 'pnr',
                        width: 60
                    }, {
                        header: '航班号',
                        dataIndex: 'air_no',
                        width: 60
                    }, {
                        header: '航程',
                        dataIndex: 'fromto',
                        width: 60
                    }, {
                        header: '乘机日期',
                        dataIndex: 'fly_date',
                        width: 70,
                        renderer: Ext.util.Format.dateRenderer('Y-m-d')
                    }, {
                        header: '舱位',
                        dataIndex: 'position',
                        width: 40
                    }, {
                        header: '起飞(T)',
                        dataIndex: 'fly_time',
                        width: 50
                    }, {
                        header: '预定时间',
                        dataIndex: 'created_at',
                        width: 130,
                        renderer: Ext.util.Format.dateRenderer('Y-m-d H:i:s')
                    }, {
                        header: '支付状态',
                        dataIndex: 'trx_status',
                        width: 70
                    }, {
                        header: '订单状态',
                        dataIndex: 'status',
                        width: 70
                    }, {
                        header: '操作',
                        dataIndex: 'fromto',
                        width: 70,
                        renderer: operationRender
                    }]);
                function operationRender(value, cellmeta, record, rowIndex, columnIndex, store){
                    if (record.get("status")!="处理完成" && record.get("status")!="申请退票"){
                        return "<a onclick=setWebOrderComplete('"+record.get("order_no")+"') target=_blank>处理完成</a>";
                    }
                }
                setWebOrderComplete = function(webOrderNo){
                    Ext.Ajax.request({
                        url: '/ticket/setOfferWebOrderComplete?order_no='+webOrderNo,
                        method: 'GET',
                        success: function(result, request){
                            if (result.responseText == "SUCCESS") {
                                webOfferOrderDS.load({
                                    params: {
                                        start: 0,
                                        limit: 10
                                    }
                                });
                            }
                        },
                        failure: function(result, request){
                            Ext.MessageBox.alert('消息', '通讯失败,请稍后再试,或联系管理员!');
                        }
                    });
                }
                //隐藏order数据库id
                webOfferOrderCM.setHidden(1, !webOfferOrderCM.isHidden(1));
                var webOfferOrderDS = new Ext.data.Store({
                    proxy: new Ext.data.HttpProxy({
                        url: '/ticket/getWebAgentOrdersBySearch'
                    }),
                    reader: new Ext.data.JsonReader({
                        totalProperty: 'totalProperty',
                        root: 'root'
                    }, [{
                            name: 'id'
                        }, {
                            name: 'order_no'
                        }, {
                            name: 'position'
                        }, {
                            name: 'pnr'
                        }, {
                            name: 'status'
                        }, {
                            name: 'trx_status'
                        }, {
                            name: 'air_no'
                        }, {
                            name: 'fly_date'
                        }, {
                            name: 'fly_time'
                        }, {
                            name: 'created_at'
                        }, {
                            name: 'fromto'
                        }])
                });

                /************************************************************
                 * Define menubar now in here
                 *   add and delete functions
                 ************************************************************/
                var ordergridpanel = new Ext.grid.GridPanel({
                    el: 'offer_web_order_div',
                    ds: webOfferOrderDS,
                    cm: webOfferOrderCM,
                    //sm: sm,
                    loadMask: true,
                    autoWidth: true,
                    collapsible: true,
                    animCollapse: true,
                    height: 380,
                    title: '订单信息(代理商)',
                    bbar: new Ext.PagingToolbar({
                        pageSize: 10,
                        store: webOfferOrderDS,
                        displayInfo: true,
                        displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
                        emptyMsg: "没有记录"
                    })
                });

                ordergridpanel.render();
                //ds.load();
                webOfferOrderDS.load({
                    params: {
                        start: 0,
                        limit: 10
                    }
                });
                var ordersrow;
                var current_order_id;
                //双击订单查看机票详细
                ordergridpanel.on('rowdblclick', function(ordergridpanel, rowIndex, e){
                    var current_order_selected = webOfferOrderDS.data.items[rowIndex].id;
                    ordersrow = webOfferOrderDS.getById(current_order_selected);//ds为绑定到grid的数据源
                    current_order_id = ordersrow.get("id");
                    ticketds.baseParams.id = current_order_id;
                    ticketds.reload();
                });

                //### 机票Grid ###
                var ticketcm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
                        header: '编号',
                        dataIndex: 'id',
                        hidden: true
                    }, {
                        header: 'PNR',
                        dataIndex: 'pnr',
                        width: 60
                    }, {
                        header: '旅客姓名',
                        dataIndex: 'pass_name',
                        width: 70
                    }, {
                        header: '证件号码',
                        dataIndex: 'pass_card_id',
                        width: 140
                    }, {
                        header: '票号',
                        dataIndex: 'ticket_no',
                        width: 110
                        /*
                    }, {
                        header: '状态',
                        dataIndex: 'status',
                        width: 50
                         */
                    }, {
                        header: '机票价格',
                        dataIndex: 'price',
                        width: 60
                    }, {
                        header: '机场费税',
                        dataIndex: 'airdrome_tax',
                        width: 60
                    }, {
                        header: '附加费',
                        dataIndex: 'fuel_tax',
                        width: 60
                        /*
                    }, {
                        header: '保险份数',
                        dataIndex: 'insurance_number',
                        width: 60
                         */
                    }, {
                        header: '其他',
                        dataIndex: 'other',
                        width: 50
                    }, {
                        header: '合计',
                        dataIndex: 'total_price',
                        width: 60
                    },{
                        header: '修改票号',
                        width: 70,
                        renderer: modTicketNoRender
                    }]);
                ticketcm.defaultSortable = true;
                var ticketds = new Ext.data.Store({
                    proxy: new Ext.data.HttpProxy({
                        url: '/ticket/getTicketsByOrderID'
                    }),
                    reader: new Ext.data.JsonReader({
                        //totalProperty: 'totalProperty',
                        root: 'root'
                    }, [{
                            name: 'id'
                        }, {
                            name: 'pnr'
                        }, {
                            name: 'web_order_id'
                        }, {
                            name: 'pass_name'
                        }, {
                            name: 'pass_tel'
                        }, {
                            name: 'pass_card_id'
                        }, {
                            name: 'ticket_no'
                        }, {
                            name: 'price'
                        }, {
                            name: 'status'
                        }, {
                            name: 'airdrome_tax'
                        }, {
                            name: 'fuel_tax'
                        }, {
                            name: 'other'
                        }, {
                            name: 'total_price'
                        }, {
                            name: 'insurance_campany'
                        }, {
                            name: 'insurance_out_date'
                        }, {
                            name: 'insurance_number'
                        }, {
                            name: 'insurance_no'
                        }, {
                            name: 'insurance_type'
                        }])
                });

                var ticketgridpanel = new Ext.grid.GridPanel({
                    el: 'queryTicketGrid',
                    ds: ticketds,
                    cm: ticketcm,
                    loadMask: true,
                    autoWidth: true,
                    collapsible: true,
                    animCollapse: true,
                    height: 180,
                    title: '乘客航班信息'
                });

                ticketgridpanel.render();
                ticketds.load();

                function modTicketNoRender(value, cellmeta, record, rowIndex, columnIndex, store){
                    if (record.get("status")=="处理完成"){
                        return ""
                    }else{
                        var cur_ticket_id = record.get("id");
                        return "<a href=# onclick='modWebTicketNo(\""+cur_ticket_id+"\")'>修改票号</a>";
                    }
                }
                modWebTicketNo = function(id){
                    var ticketNo = new Ext.form.TextField({
                        fieldLabel: '票号',
                        name: 'ticket_no',
                        allowBlank: true,
                        anchor: '90%'
                    });

                    var formPanel = new Ext.form.FormPanel({
                        //baseCls: 'x-plain',
                        labelWidth: 60,
                        labelAlign: 'right',
                        frame: true,
                        url: "/ticket/saveEditTicket?id=" + id,
                        items: [ticketNo]
                    });

                    var fixWindow = new Ext.Window({
                        title: '编辑票号',
                        width: 260,
                        height: 130,
                        layout: 'fit',
                        plain: true,
                        bodyStyle: 'padding:5px;',
                        buttonAlign: 'center',
                        items: formPanel,
                        buttons: [{
                                text: '保存',
                                handler: function(){
                                    // check form value
                                    if (formPanel.form.isValid()) {
                                        var go = false;
                                        var t_no = ticketNo.getValue();
                                        if (t_no.length == 13 || (t_no.length == 14 && t_no.indexOf("-") == 3)) {
                                            go = true;
                                        }
                                        else {
                                            Ext.MessageBox.alert('提示', "票号不符合规范");
                                        }
                                        if(go){
                                            formPanel.form.submit({
                                                waitMsg: 'In processing',
                                                success: function(form, action){
                                                    var info = "操作失败，具体失败原因请查询系统日志";
                                                    if (action.result && action.result.info && action.result.info != "") {
                                                        info = action.result.info;
                                                    }
                                                    if (info.indexOf("成功") >= 0) {
                                                        Ext.MessageBox.alert('编辑成功', info);
                                                        fixWindow.hide();
                                                        ticketds.reload();
                                                    }else{
                                                        Ext.MessageBox.alert('编辑失败', info);
                                                    }
                                                },
                                                failure: function(form, action){
                                                    Ext.MessageBox.alert('操作信息', '操作失败');
                                                }
                                            });
                                        }
                                    }
                                }
                            }, {
                                text: '取消',
                                handler: function(){
                                    fixWindow.hide();
                                }
                            }]
                    });
                    fixWindow.show();
                }
            });

        </script>
    </body>
</html>
